/**
 * 承包队设置 编辑页
 *
 * 作者 admin
 * 日期 2024-07-29 14:19:05
 */
layui.use(['jquery','febs', 'form', 'commonJS', 'table','validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        $view = $('#baseContractorTeam-edit'),
        $searchForm = $view.find('form'),
        validate = layui.validate;

    let $constructionDept = $view.find("#constructionDept");
    let tenantId = POPUP_DATA.tenantId
    let tenantName = POPUP_DATA.tenantName
    let contractorId = POPUP_DATA.contractorId
    let contractorName = POPUP_DATA.contractorName
    form.verify(validate);
    form.render();
    initFormValue();


    function initFormValue() {
        if (baseContractorTeamData) {
            form.val("baseContractorTeam-edit-form", {
                "contractorTeamId": baseContractorTeamData.contractorTeamId,
                "tenantId": baseContractorTeamData.tenantId,
                "contractorId": baseContractorTeamData.contractorId,
                "constructionDeptId": baseContractorTeamData.constructionDeptId,
                "constructionDept": baseContractorTeamData.constructionDept,
                "teamName": baseContractorTeamData.teamName,
                "remark": baseContractorTeamData.remark,
                "tenantName": baseContractorTeamData.tenantName,
            });
            $("#tenantName").text(baseContractorTeamData.tenantName);
            $('#contractorName').text(baseContractorTeamData.contractorName)
            //根基后台status值渲染开关
            if(baseContractorTeamData.status === '1'){
                $view.find('input[type=checkbox]').attr("checked","true");
                form.render('checkbox');
            }


        }else {
            form.val("baseContractorTeam-edit-form", {
                "tenantId": tenantId,
                "tenantName": tenantName,
                "contractorId":contractorId,
            });
            $view.find('input[type=checkbox]').attr("checked","true");
            form.render('checkbox');
            $("#tenantName").text(tenantName);
            $('#contractorName').text(contractorName)
        }

    }

    form.on('submit(baseContractorTeam-edit-form-submit)', function (data) {
        if(data.field.status == "on"){
            data.field.status = '1';
        }else{
            data.field.status = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    $constructionDept.bind("click",function(){
        febs.modal.open('选择部门', '/common/dept', {
            btn: ['提交','取消'],
            area: ['500px','800px'],
            data: {
                tenantId: tenantId
            },
            yes: function (index, layero) {
                let deptData = null
                if(typeof getSelectData == "function"){
                    deptData = getSelectData()
                }
                if(deptData){
                    $view.find("input[name='constructionDeptId']").val(deptData.deptId)
                    $view.find("input[name='constructionDept']").val(deptData.deptName)
                }
                layer.close(index);
                return false;
            },
            btn2: function (index) {
                layer.close(index);
            },
        });
    })
    function addOrUpdate(data) {
        if (data.contractorTeamId && data.contractorTeamId.length > 0) {
            febs.modal.confirm('修改承包队', '确定承修改包队？', function () {
                febs.postArray(ctx + 'baseContractorTeam/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-baseContractor').find('#query').trigger('click');
                });
            });


        } else {
            febs.modal.confirm('新增承包队', '确定新增承包队？', function () {
                febs.postArray(ctx + 'baseContractorTeam', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-baseContractor').find('#query').trigger('click');
                });
            });

        }
    }




});